package cn.huaqingcheng.tianshu.core.auth.jwt;

import com.auth0.jwt.interfaces.ECDSAKeyProvider;
import lombok.EqualsAndHashCode;

import java.security.KeyPair;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;

/**
 * KeyFileProvider
 */
@EqualsAndHashCode
class KeyFileProvider implements ECDSAKeyProvider {

    private final JwtProperties.KeyFileProperties properties;

    private final ECPrivateKey privateKey;

    private final ECPublicKey publicKey;


    public KeyFileProvider(JwtProperties properties) throws Exception {
        this.properties = properties.getKey();
        KeyPair pair = ECKeyPersistence.loadKeys(this.properties.getPrivatePath(), this.properties.getPublicPath());
        privateKey = (ECPrivateKey) pair.getPrivate();
        publicKey = (ECPublicKey) pair.getPublic();
    }

    @Override
    public ECPublicKey getPublicKeyById(String keyId) {
        return publicKey;
    }

    @Override
    public ECPrivateKey getPrivateKey() {
        return privateKey;
    }

    @Override
    public String getPrivateKeyId() {
        return properties.getKeyId();
    }

}
